package com.easy.generator.java;

/**
 * @author easy
 * @date 2025年07月05日 08:41
 */

import com.easy.generator.Easy;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;

public class ResultGenerator {
    private static final String CLASS_NAME = "Result";
    private static final String CLASS_TEMPLATE =
            "package " + Easy.UTIL_PACKAGE + ";\n" +
                    "import lombok.AllArgsConstructor;\n" +
                    "import lombok.Data;\n" +
                    "import lombok.NoArgsConstructor;\n" +
                    "@Data\n" +
                    "@NoArgsConstructor\n" +
                    "public class " + CLASS_NAME + "<T> {\n" +
                    "            private int code;\n" +
                    "            private String msg;\n" +
                    "            private T data;\n" +
                    "            private long timestamp;\n" +
                    "            public static <DT> " + CLASS_NAME + "<DT> success() {\n" +
                    "                return new " + CLASS_NAME + "<>(200, \"\", null);\n" +
                    "            }\n" +
                    "            public static <DT> " + CLASS_NAME + "<DT> success(DT data) {\n" +
                    "                return new " + CLASS_NAME + "<>(200, \"\", data);\n" +
                    "            }\n" +
                    "            public static <DT> " + CLASS_NAME + "<DT> success(int code,DT data) {\n" +
                    "                return new " + CLASS_NAME + "<>(code, \"\", data);\n" +
                    "            }\n" +
                    "            public static <DT> " + CLASS_NAME + "<DT> success(int code,String msg,DT data) {\n" +
                    "                return new " + CLASS_NAME + "<>(code, msg, data);\n" +
                    "            }\n" +
                    "            public static <DT> " + CLASS_NAME + "<DT> success(String msg,DT data) {\n" +
                    "                return new " + CLASS_NAME + "<>(200, msg, data);\n" +
                    "            }\n" +
                    "            public static <DT> " + CLASS_NAME + "<DT> error(int code, String msg) {\n" +
                    "                return new " + CLASS_NAME + "<>(code, msg, null);\n" +
                    "            }\n" +
                    "            public " + CLASS_NAME + "(int code,String msg,T data) {\n" +
                    "                this.code = code;\n" +
                    "                this.msg = msg;\n" +
                    "                this.data = data;\n" +
                    "                this.timestamp = System.currentTimeMillis();\n" +
                    "            }" +
                    "        }";

    public static void generate() throws IOException {
        Easy.initPackage(Easy.UTIL_PACKAGE);
        String content = String.format(CLASS_TEMPLATE, LocalDate.now());
        File outputFile = new File(Easy.PROJECT_ABSOLUTE_URL + Easy.SRC_DIR + Easy.convertPackagePath(Easy.UTIL_PACKAGE) + "/" + CLASS_NAME + ".java");
        try (FileWriter writer = new FileWriter(outputFile)) {
            writer.write(content);
        }
    }

}
